<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>通过执行代码字符串创建动态函数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.call-user-func.html">« call_user_func</a></li>
      <li style="float: right;"><a href="function.forward-static-call-array.html">forward_static_call_array »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.funchand.html">函数处理 函数</a></li>
    <li>通过执行代码字符串创建动态函数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.create-function" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">create_function</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">create_function</span> &mdash; <span class="dc-title">通过执行代码字符串创建动态函数</span></p>

 </div>

 <div id="function.create-function-refsynopsisdiv">
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">本函数已自 PHP 7.2.0
 起被<em class="emphasis">废弃</em>，并自 PHP 8.0.0 起被<em class="emphasis">移除</em>。
 强烈建议不要依赖本函数。</p></div>
 </div>

 <div class="refsect1 description" id="refsect1-function.create-function-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>create_function</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$args</code></span>, <span class="methodparam"><span class="type">string</span> <code class="parameter">$code</code></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   从传递的参数中创建动态参数，并返回它的唯一名字。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
   <p class="para">
    此函数在内部执行 <span class="function"><a href="function.eval.html" class="function">eval()</a></span>，因此具有跟 <span class="function"><a href="function.eval.html" class="function">eval()</a></span>
    相同的安全问题。还有性能以及内存使用较差的特性，因为创建的函数是全局的且无法释放。
   </p>
   <p class="para">
    应该使用原生<a href="functions.anonymous.html" class="link">匿名函数</a>。
   </p>
  </div>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.create-function-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   It is normally advisable to pass these parameters as
   <a href="language.types.string.html#language.types.string.syntax.single" class="link">single quoted</a>
   strings. If using <a href="language.types.string.html#language.types.string.syntax.double" class="link">double quoted</a>
   strings, variable names in the code need to be escaped carefully, e.g.
   <code class="literal">\$somevar</code>.
   <dl>
    
     <dt>
<code class="parameter">args</code></dt>

     <dd>

      <p class="para">
       The function arguments, as a single comma-separated string.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">code</code></dt>

     <dd>

      <p class="para">
       The function code.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.create-function-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a unique function name as a string,  或者在失败时返回 <strong><code>false</code></strong>.
   Note that the name contains a non-printable character (<code class="literal">&quot;\0&quot;</code>),
   so care should be taken when printing the name or incorporating it in any other
   string.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.create-function-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5078">
    <p><strong>示例 #1 
     Creating a function dynamically, with <span class="function"><strong>create_function()</strong></span>
     or anonymous functions
    </strong></p>
    <div class="example-contents"><p>
     You can use a dynamically created function, to (for example) create a function from
     information gathered at run time. First, using <span class="function"><strong>create_function()</strong></span>:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$newfunc&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'return&nbsp;"ln($a)&nbsp;+&nbsp;ln($b)&nbsp;=&nbsp;"&nbsp;.&nbsp;log($a&nbsp;*&nbsp;$b);'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$newfunc</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">M_E</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     Now the same code, using an <a href="functions.anonymous.html" class="link">anonymous function</a>;
     note that the code and arguments are no longer contained in strings:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$newfunc&nbsp;</span><span style="color: #007700">=&nbsp;function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{&nbsp;return&nbsp;</span><span style="color: #DD0000">"ln(</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">)&nbsp;+&nbsp;ln(</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">)&nbsp;=&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">*&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">);&nbsp;};<br />echo&nbsp;</span><span style="color: #0000BB">$newfunc</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">M_E</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
ln(2) + ln(2.718281828459) = 1.6931471805599
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5079">
    <p><strong>示例 #2 
     Making a general processing function, with <span class="function"><strong>create_function()</strong></span>
     or anonymous functions
    </strong></p>
    <div class="example-contents"><p>
     Another use could be to have general handler function that can apply a set
     of operations to a list of parameters:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">process</span><span style="color: #007700">(</span><span style="color: #0000BB">$var1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$var2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$farr</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$farr&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$f</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$f</span><span style="color: #007700">(</span><span style="color: #0000BB">$var1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$var2</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;a&nbsp;bunch&nbsp;of&nbsp;math&nbsp;functions<br /></span><span style="color: #0000BB">$farr&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$x,$y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'return&nbsp;"some&nbsp;trig:&nbsp;".(sin($x)&nbsp;+&nbsp;$x*cos($y));'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$x,$y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'return&nbsp;"a&nbsp;hypotenuse:&nbsp;".sqrt($x*$x&nbsp;+&nbsp;$y*$y);'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'if&nbsp;($a&nbsp;&gt;=0)&nbsp;{return&nbsp;"b*a^2&nbsp;=&nbsp;".$b*sqrt($a);}&nbsp;else&nbsp;{return&nbsp;false;}'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"return&nbsp;\"min(b^2+a,&nbsp;a^2,b)&nbsp;=&nbsp;\".min(\$a*\$a+\$b,\$b*\$b+\$a);"</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'if&nbsp;($a&nbsp;&gt;&nbsp;0&nbsp;&amp;&amp;&nbsp;$b&nbsp;!=&nbsp;0)&nbsp;{return&nbsp;"ln(a)/b&nbsp;=&nbsp;".log($a)/$b;&nbsp;}&nbsp;else&nbsp;{&nbsp;return&nbsp;false;&nbsp;}'</span><span style="color: #007700">)<br />);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\nUsing&nbsp;the&nbsp;first&nbsp;array&nbsp;of&nbsp;dynamic&nbsp;functions\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"parameters:&nbsp;2.3445,&nbsp;M_PI\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">process</span><span style="color: #007700">(</span><span style="color: #0000BB">2.3445</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">M_PI</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$farr</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;now&nbsp;make&nbsp;a&nbsp;bunch&nbsp;of&nbsp;string&nbsp;processing&nbsp;functions<br /></span><span style="color: #0000BB">$garr&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$b,$a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'if&nbsp;(strncmp($a,&nbsp;$b,&nbsp;3)&nbsp;==&nbsp;0)&nbsp;return&nbsp;"**&nbsp;\"$a\"&nbsp;'</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'and&nbsp;\"$b\"\n**&nbsp;Look&nbsp;the&nbsp;same&nbsp;to&nbsp;me!&nbsp;(looking&nbsp;at&nbsp;the&nbsp;first&nbsp;3&nbsp;chars)";'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'return&nbsp;"CRCs:&nbsp;"&nbsp;.&nbsp;crc32($a)&nbsp;.&nbsp;",&nbsp;".crc32($b);'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'return&nbsp;"similar(a,b)&nbsp;=&nbsp;"&nbsp;.&nbsp;similar_text($a,&nbsp;$b,&nbsp;$p)&nbsp;.&nbsp;"($p%)";'</span><span style="color: #007700">)<br />);<br />echo&nbsp;</span><span style="color: #DD0000">"\nUsing&nbsp;the&nbsp;second&nbsp;array&nbsp;of&nbsp;dynamic&nbsp;functions\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">process</span><span style="color: #007700">(</span><span style="color: #DD0000">"Twas&nbsp;brilling&nbsp;and&nbsp;the&nbsp;slithy&nbsp;toves"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Twas&nbsp;the&nbsp;night"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$garr</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     Again, here is the same code using
     <a href="functions.anonymous.html" class="link">anonymous functions</a>.
     Note that variable names in the code no longer need to be escaped,
     because they are not enclosed in a string.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">process</span><span style="color: #007700">(</span><span style="color: #0000BB">$var1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$var2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$farr</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$farr&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$f</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$f</span><span style="color: #007700">(</span><span style="color: #0000BB">$var1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$var2</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;a&nbsp;bunch&nbsp;of&nbsp;math&nbsp;functions<br /></span><span style="color: #0000BB">$farr&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;function(</span><span style="color: #0000BB">$x</span><span style="color: #007700">,</span><span style="color: #0000BB">$y</span><span style="color: #007700">)&nbsp;{&nbsp;return&nbsp;</span><span style="color: #DD0000">"some&nbsp;trig:&nbsp;"</span><span style="color: #007700">.(</span><span style="color: #0000BB">sin</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">)&nbsp;+&nbsp;</span><span style="color: #0000BB">$x</span><span style="color: #007700">*</span><span style="color: #0000BB">cos</span><span style="color: #007700">(</span><span style="color: #0000BB">$y</span><span style="color: #007700">));&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;function(</span><span style="color: #0000BB">$x</span><span style="color: #007700">,</span><span style="color: #0000BB">$y</span><span style="color: #007700">)&nbsp;{&nbsp;return&nbsp;</span><span style="color: #DD0000">"a&nbsp;hypotenuse:&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">sqrt</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">*</span><span style="color: #0000BB">$x&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$y</span><span style="color: #007700">*</span><span style="color: #0000BB">$y</span><span style="color: #007700">);&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{&nbsp;if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;=</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{return&nbsp;</span><span style="color: #DD0000">"b*a^2&nbsp;=&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">$b</span><span style="color: #007700">*</span><span style="color: #0000BB">sqrt</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);}&nbsp;else&nbsp;{return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;}&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{&nbsp;return&nbsp;</span><span style="color: #DD0000">"min(b^2+a,&nbsp;a^2,b)&nbsp;=&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">min</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">*</span><span style="color: #0000BB">$a</span><span style="color: #007700">+</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">*</span><span style="color: #0000BB">$b</span><span style="color: #007700">+</span><span style="color: #0000BB">$a</span><span style="color: #007700">);&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{&nbsp;if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">!=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{return&nbsp;</span><span style="color: #DD0000">"ln(a)/b&nbsp;=&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)/</span><span style="color: #0000BB">$b</span><span style="color: #007700">;&nbsp;}&nbsp;else&nbsp;{&nbsp;return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;&nbsp;}&nbsp;}<br />);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\nUsing&nbsp;the&nbsp;first&nbsp;array&nbsp;of&nbsp;dynamic&nbsp;functions\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"parameters:&nbsp;2.3445,&nbsp;M_PI\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">process</span><span style="color: #007700">(</span><span style="color: #0000BB">2.3445</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">M_PI</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$farr</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;now&nbsp;make&nbsp;a&nbsp;bunch&nbsp;of&nbsp;string&nbsp;processing&nbsp;functions<br /></span><span style="color: #0000BB">$garr&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;function(</span><span style="color: #0000BB">$b</span><span style="color: #007700">,</span><span style="color: #0000BB">$a</span><span style="color: #007700">)&nbsp;{&nbsp;if&nbsp;(</span><span style="color: #0000BB">strncmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">)&nbsp;==&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;return&nbsp;</span><span style="color: #DD0000">"**&nbsp;\"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">\"&nbsp;"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"and&nbsp;\"</span><span style="color: #0000BB">$b</span><span style="color: #DD0000">\"\n**&nbsp;Look&nbsp;the&nbsp;same&nbsp;to&nbsp;me!&nbsp;(looking&nbsp;at&nbsp;the&nbsp;first&nbsp;3&nbsp;chars)"</span><span style="color: #007700">;&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{&nbsp;return&nbsp;</span><span style="color: #DD0000">"CRCs:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">crc32</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">",&nbsp;"</span><span style="color: #007700">.</span><span style="color: #0000BB">crc32</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);&nbsp;},<br />&nbsp;&nbsp;&nbsp;&nbsp;function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{&nbsp;return&nbsp;</span><span style="color: #DD0000">"similar(a,b)&nbsp;=&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">similar_text</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$p</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"(</span><span style="color: #0000BB">$p</span><span style="color: #DD0000">%)"</span><span style="color: #007700">;&nbsp;}<br />);<br />echo&nbsp;</span><span style="color: #DD0000">"\nUsing&nbsp;the&nbsp;second&nbsp;array&nbsp;of&nbsp;dynamic&nbsp;functions\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">process</span><span style="color: #007700">(</span><span style="color: #DD0000">"Twas&nbsp;brilling&nbsp;and&nbsp;the&nbsp;slithy&nbsp;toves"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Twas&nbsp;the&nbsp;night"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$garr</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Using the first array of dynamic functions
parameters: 2.3445, M_PI
some trig: -1.6291725057799
a hypotenuse: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a)/b = 0.27122299212594

Using the second array of dynamic functions
** &quot;Twas the night&quot; and &quot;Twas brilling and the slithy toves&quot;
** Look the same to me! (looking at the first 3 chars)
CRCs: 3569586014, 342550513
similar(a,b) = 11(45.833333333333%)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5080">
    <p><strong>示例 #3 Using dynamic functions as callback functions</strong></p>
    <div class="example-contents"><p>
     Perhaps the most common use for dynamic functions
     is to pass them as callbacks, for example when using
     <span class="function"><a href="function.array-walk.html" class="function">array_walk()</a></span> or <span class="function"><a href="function.usort.html" class="function">usort()</a></span>.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$av&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"the&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"a&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"that&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"this&nbsp;"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$av</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'&amp;$v,$k'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'$v&nbsp;=&nbsp;$v&nbsp;.&nbsp;"mango";'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$av</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     Converted to an <a href="functions.anonymous.html" class="link">anonymous function</a>:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$av&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"the&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"a&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"that&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"this&nbsp;"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$av</span><span style="color: #007700">,&nbsp;function(&amp;</span><span style="color: #0000BB">$v</span><span style="color: #007700">,</span><span style="color: #0000BB">$k</span><span style="color: #007700">)&nbsp;{&nbsp;</span><span style="color: #0000BB">$v&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$v&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"mango"</span><span style="color: #007700">;&nbsp;});<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$av</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
  [0] =&gt; the mango
  [1] =&gt; a mango
  [2] =&gt; that mango
  [3] =&gt; this mango
)
</pre></div>
    </div>
    <div class="example-contents"><p>
     Sorting strings from longest to shortest with <span class="function"><strong>create_function()</strong></span>:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sv&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"small"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"a&nbsp;big&nbsp;string"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"larger"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"it&nbsp;is&nbsp;a&nbsp;string&nbsp;thing"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Original:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$sv</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Sorted:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">usort</span><span style="color: #007700">(</span><span style="color: #0000BB">$sv</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(</span><span style="color: #DD0000">'$a,$b'</span><span style="color: #007700">,</span><span style="color: #DD0000">'return&nbsp;strlen($b)&nbsp;-&nbsp;strlen($a);'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$sv</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     Converted to an <a href="functions.anonymous.html" class="link">anonymous function</a>:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sv&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"small"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"a&nbsp;big&nbsp;string"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"larger"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"it&nbsp;is&nbsp;a&nbsp;string&nbsp;thing"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Original:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$sv</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Sorted:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">usort</span><span style="color: #007700">(</span><span style="color: #0000BB">$sv</span><span style="color: #007700">,&nbsp;function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{&nbsp;return&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);&nbsp;});<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$sv</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Original:
Array
(
  [0] =&gt; small
  [1] =&gt; a big string
  [2] =&gt; larger
  [3] =&gt; it is a string thing
)
Sorted:
Array
(
  [0] =&gt; it is a string thing
  [1] =&gt; a big string
  [2] =&gt; larger
  [3] =&gt; small
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.create-function-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="functions.anonymous.html" class="link">匿名函数</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>